package com.yksj.healthtalk.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.support.v4.app.FragmentActivity;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

import com.yksj.healthtalk.db.ChatUserHelper;
import com.yksj.healthtalk.entity.BaseInfoEntity;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.app.HTalkApplication;

public class MessageUtils {
	
	/**
	 * 
	 */
	public String deleteMessageFromUserID(final String userID,FragmentActivity activity){

			final ConcurrentHashMap<String,List<MessageEntity>> map = HTalkApplication.getAppData().messageCllection;
			//有消息需要删除消息
			if(map.containsKey(userID)){
				List<MessageEntity> list = map.get(userID);
				final JSONArray buffer=new JSONArray();
				for (MessageEntity messageEntity : list) {
					String id = messageEntity.getServerId();
					if(id == null)continue;
					buffer.put(id);
				}
				HttpRestClient.doHttpDeleteLeaveOnlineMessage(SmartFoxClient.getLoginUserId(),buffer.toString(),new AsyncHttpResponseHandler(activity){
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						JSONObject jsonObject;
						try {
							jsonObject = new JSONObject(content);
							if(1==jsonObject.optInt("error_code",-1)){//删除成功
								final List<MessageEntity> list = HTalkApplication.getAppData().messageCllection.get(userID);
								if(list != null)HTalkApplication.getAppData().messageCllection.remove(userID);
								
								List<String> strings=new ArrayList<String>();
								for (int i = 0; i < buffer.length(); i++) {
									HTalkApplication.getAppData().messageMap.remove(buffer.get(i).toString());
									strings.add(buffer.get(i).toString());
								}
								ChatUserHelper.getInstance().updateChatMesageDeleteState(strings);
							}else{
								ToastUtil.showToastPanl(jsonObject.optString("error_message","删除失败"));
							}
							
						} catch (JSONException e) {
						}
					}
				});
			}
			return userID;
	}
}
